package com.demo.autotest.service;

import com.demo.autotest.common.constant.PermissionEnum;
import com.demo.autotest.database.domain.*;
import com.demo.autotest.database.mapper.PermissionMapper;
import com.demo.autotest.database.mapper.ProjectMapper;
import com.demo.autotest.database.mapper.RoleMapper;
import com.demo.autotest.database.mapper.UserMapper;
import com.demo.autotest.dto.UserDTO;
import com.demo.autotest.request.QueryRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service    // 定义service层
@Transactional(rollbackFor = Exception.class)
public class ProjectService {

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private RoleMapper roleMapper;

    public void saveProject(Project project){
        // 验证account、mobile是否已经存在数据库中 todo

        if(project.getId() ==null || project.getId().equals("")){
            project.setId(UUID.randomUUID().toString());
            project.setCreateTime(System.currentTimeMillis());
            project.setUpdateTime(System.currentTimeMillis());
            projectMapper.insertProject(project);

            Role adminRole = new Role();
            adminRole.setId(UUID.randomUUID().toString());
            adminRole.setName("项目管理员");
            adminRole.setProjectId(project.getId());
            adminRole.setCreateTime(System.currentTimeMillis());
            adminRole.setUpdateTime(System.currentTimeMillis());
            roleMapper.insertRole(adminRole);

            Role normalRole = new Role();
            normalRole.setId(UUID.randomUUID().toString());
            normalRole.setName("项目普通用户");
            normalRole.setProjectId(project.getId());
            normalRole.setCreateTime(System.currentTimeMillis());
            normalRole.setUpdateTime(System.currentTimeMillis());
            roleMapper.insertRole(normalRole);

            List<RolePermission> rolePermissions = new ArrayList<>();
            for (PermissionEnum permissionEnum: PermissionEnum.values()){
                if (permissionEnum.id.equals(PermissionEnum.PROJECT_MENU)){
                    continue;
                }
                if (permissionEnum.id.equals(PermissionEnum.NORMAL_MENU) || permissionEnum.equals(PermissionEnum.SETTING_MENU)){
                    rolePermissions.add(this.getRolePermission(adminRole.getId(), permissionEnum.id));
                    rolePermissions.add(this.getRolePermission(normalRole.getId(), permissionEnum.id));
                }
                else{
                    rolePermissions.add(this.getRolePermission(adminRole.getId(), permissionEnum.id));
                }
            }
            roleMapper.insertRolePermissions(rolePermissions);
        }else {
            project.setUpdateTime(System.currentTimeMillis());
            projectMapper.updateProject(project);
        }
    }

    private RolePermission getRolePermission(String roleId, String permissionId){
        RolePermission rolePermission = new RolePermission();
        rolePermission.setId(UUID.randomUUID().toString());
        rolePermission.setRoleId(roleId);
        rolePermission.setPermissionId(permissionId);
        rolePermission.setCreateTime(System.currentTimeMillis());
        rolePermission.setUpdateTime(System.currentTimeMillis());
        return rolePermission;
    }

    public void deleteProject(String id){
        projectMapper.deleteProject(id);
    }

    public void recoverProject(String id){
        projectMapper.recoverProject(id);
    }

    public UserDTO switchProject(UserProject userProject){
        userMapper.updateUserProject(userProject);
        UserDTO userDTO = userMapper.getUserById(userProject.getUserId());
        List<String> permissions = permissionMapper.getPermissionByProjectAndUser(userDTO.getLastProject(), userDTO.getId());
        userDTO.setPermissions(permissions);
        userDTO.setPassword(null);
        return userDTO;
    }

    public List<Project> getProjectList(QueryRequest request){
        if (request.getCondition() != null && !request.getCondition().equals("")){
            request.setCondition("%"+request.getCondition()+"%");
        }
        return projectMapper.getProjectList(request);
    }

}
